package org.example.t0526;

public class T3 {

    public static void main(String[] args) {
        final Object lock = new Object();
        char [] a1 = "1234567".toCharArray();
        char [] a2 = "ABCDEFG".toCharArray();

        new Thread(() ->{
            synchronized (lock){
                for(char a : a1){
                    System.out.print(a);
                    lock.notify();//唤醒等待队列中的任意一个线程
                    try {
                        lock.wait();//让当前线程让出锁，进入等待队列
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notify();//必须叫醒所有线程，否则无法停止;因为线程之间在互相wait，不调用notify的话，则不停止
            }

        },"t1").start();

        new Thread(() ->{
            synchronized (lock){
                for(char a : a2){
                    System.out.print(a);
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                    lock.notify();
                }
            }

        },"t2").start();
    }
}
